package com.wwt.server.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwt.server.mapper.MenuRoleMapper;
import com.wwt.server.pojo.MenuRole;
import com.wwt.server.service.IMenuRoleService;
import com.wwt.server.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tao
 * @since 2021-12-05
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {
    @Autowired
    private MenuRoleMapper menuRoleMapper;
    /**
     * 更新角色所拥有的菜单
     * @param rid
     * @param mids
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Result updateMenuRole(Integer rid, Integer[] mids) {
        //先删除角色原有菜单
        QueryWrapper<MenuRole> wrapper = new QueryWrapper<MenuRole>().eq("rid", rid);
        int count = menuRoleMapper.delete(wrapper);
        if (mids.length > 0 || mids == null) {
            if (count != 0)
                return Result.success("更新成功！");
            return Result.fail("更新失败，请重试！");
        }
        //添加新的菜单列表
        Integer integer = menuRoleMapper.batchInsert(rid, mids);
        if (integer != 0)
            return Result.success("更新成功！");
        return Result.fail("更新失败，请重试！");
    }
}
